/**
 * Copyright (C), 2021, 因特驰
 * FileName: ErshoucheController
 * Author:   李晋武
 * Date:     2021/7/12 20:06
 * Description:
 * History:
 * 李晋武          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.esc.controller;

import com.esc.model.Guanggao;
import com.esc.model.Shouye;
import com.esc.service.GuanggaoService;
import com.esc.service.ShouyeService;
import com.esc.util.PageResult;
import com.esc.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * +
 *
 * @author Yun
 * @create 2021/7/12
 * @since 1.0.0
 */
@Controller
@RequestMapping("shouye")
public class ShouyeController {


    @Autowired
    private ShouyeService shouyeService;
    @Autowired
    private GuanggaoService guanggaoService;

    @RequestMapping("index")
    public String index(Model model, HttpServletRequest request){

        //加载广告
        Guanggao guanggao1 = guanggaoService.getGuanggaoimgById(1);
        Guanggao guanggao2 = guanggaoService.getGuanggaoimgById(2);
        model.addAttribute("guanggao1",guanggao1);
        model.addAttribute("guanggao2",guanggao2);

        //加载列表
        PageUtil<Shouye> pageUtil = new PageUtil<Shouye>();
        pageUtil.setPage(1);
        pageUtil.setRows(12);
        PageResult pageResult = shouyeService.queryShouyeList(pageUtil);
        model.addAttribute("shouyeList",pageResult.getRows());

        //买家登录
        String buyerName = (String) request.getSession().getAttribute("buyerName");
        if (buyerName!=null){
            model.addAttribute("buyerName",buyerName);
            //request.getSession().setAttribute("sellerName","");
        }else {
            model.addAttribute("buyerName","");
        }
        //卖家登录
        String sellerName = (String) request.getSession().getAttribute("sellerName");
        if (sellerName!=null){
            model.addAttribute("sellerName",sellerName);
            //request.getSession().setAttribute("buyerName","");
        }else {
            model.addAttribute("sellerName","");
        }

        return "shouye/index";
    }

    @RequestMapping("getGuanggaoimgById")
    @ResponseBody
    public String getGuanggaoimgById(Integer guanggaoid){
        Guanggao guanggao = guanggaoService.getGuanggaoimgById(guanggaoid);
        return guanggao.getGuanggaoimg();
    }
}